package com.atguigu.jue.demo01;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Ticket{
    private int number = 50;
//    public synchronized void sale(){
//        if (number>0){
//            System.out.println(Thread.currentThread().getName()+"\t"+"卖出第:"+(number--)+"\t"+"还剩下:"+number);
//
//        }
//    }
    //-------------------------------------JUC
    Lock lock=new ReentrantLock();
    public void sale(){
         lock.lock();
                 try{if (number>0){
                     System.out.println(Thread.currentThread().getName()+"\t"+"卖出第:"+(number--)+"\t"+"还剩下:"+number);
                 }
                 }finally {
                     lock.unlock();
                 }
    }

}

/**
 * @auther zzyy
 * @create 2020-03-25 12:41
 * 三个售票员    卖出      50张票
 * 如何编写企业级需要的工程化代码？
 *
 * 1 在高内聚低耦合的前提下，线程 操作  资源类,多线程开发中，一言不合先new一个资源类,线程对资源类进行什么样的操作？
 */
public class SellTicket {


    public static void main(String[] args) {

        Ticket ticket=new Ticket();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                for (int i = 0; i < 51; i++) {
//                    ticket.sale();
//                }
//            }
//        },"A").start();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                for (int i = 0; i < 51; i++) {
//                    ticket.sale();
//                }
//            }
//        },"B").start();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                for (int i = 0; i < 51; i++) {
//                    ticket.sale();
//                }
//            }
//        },"C").start();
        new Thread(()->{ for (int i = 0; i < 51; i++)ticket.sale();},"A").start();
        new Thread(()->{ for (int i = 0; i < 51; i++)ticket.sale();},"B").start();
        new Thread(()->{ for (int i = 0; i < 51; i++)ticket.sale();},"C").start();
    }

}
